package cn.wolfcode.wolf2w.util;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class AssertUtil {

    public static void hasLength(String value, String msg) {
        if (!StringUtils.hasLength(value)) {
            throw new RuntimeException(msg);
        }
    }

    //判断两次密码是否相等
    public static void isEquals(String password, String rpassword) {
        if (password == null || rpassword == null) {
            throw new RuntimeException("两次输入的密码为空");
        }
        if (!password.equals(rpassword)) {
            throw new RuntimeException("两次输入密码不相等");
        }
    }

    //判断手机号是否符合格式

    /**
     * @param phone 传入的手机号
     * @return
     * @throws PatternSyntaxException
     */
    public static void isPhone(String phone) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);

        if (!m.matches()) {
            throw new RuntimeException("手机号格式不正确");
        }
    }
}
